<?php

class Application_Model_Mapper_TurnpunkteMapper
{
	protected $_dbTable;

	public function setDbTable ($dbTable)
	{
		
		if (is_string($dbTable))
		{
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract)
		{
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable ()
	{
		
		if (null === $this->_dbTable)
		{
			$this->setDbTable('Application_Model_DbTable_Turnpunkte');
		}
		return $this->_dbTable;
		
	}

	public function add(Application_Model_Turnpunkte $turnpunkt)
	{
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
		
			$data = array(
					'id' => $generatedid,
					'turn' => $turnpunkt->getTurn(),
					'position' => $turnpunkt->getPosition(),
					'lat' => $turnpunkt->getLat(),
					'lng' => $turnpunkt->getLng(),
					'referenzid' => $turnpunkt->getReferenzid(),
					'referenzobjekt' => $turnpunkt->getReferenzobjekt(),
					'name' => $turnpunkt->getName(),
					'status' => $turnpunkt->getStatus()
			);
	
			return $this->getDbTable()->insert($data);
		}
		else{
			$this->add($turnpunkt);
		}
		
	}
	
	public function editarray($turnpunkte, $turn)
	{
		
		for($i = 0; $i <count($turnpunkte);++$i){
			$turnpunkt = new Application_Model_Turnpunkte();
			$turnpunkt->setLat($turnpunkte[$i]['latlng']['$a']);
			$turnpunkt->setLng($turnpunkte[$i]['latlng']['ab']);
			$turnpunkt->setPosition($turnpunkte[$i]['position']);
			$turnpunkt->setReferenzid($turnpunkte[$i]['objektid']);
			$turnpunkt->setReferenzobjekt($turnpunkte[$i]['objekttyp']);
			$turnpunkt->setName($turnpunkte[$i]['name']);
			$turnpunkt->setStatus($turnpunkte[$i]['status']);
			$turnpunkt->setTurn($turn);
			$this->edit($turnpunkt, $turnpunkte[$i]['position']);
		}
	}

	public function addarray($turnpunkte, $turn)
	{		
		for($i = 0; $i <count($turnpunkte);++$i){
			$turnpunkt = new Application_Model_Turnpunkte();
			$turnpunkt->setLat($turnpunkte[$i]['latlng']['$a']);
			$turnpunkt->setLng($turnpunkte[$i]['latlng']['ab']);
			$turnpunkt->setPosition($turnpunkte[$i]['position']);
			$turnpunkt->setReferenzid($turnpunkte[$i]['objektid']);
			$turnpunkt->setReferenzobjekt($turnpunkte[$i]['objekttyp']);
			$turnpunkt->setName($turnpunkte[$i]['name']);
			$turnpunkt->setStatus($turnpunkte[$i]['status']);
			$turnpunkt->setTurn($turn);
			$this->add($turnpunkt);
		}
	}
	
	public function update (Application_Model_Turnpunkte $turn)
	{
		$data = array(
				'name' => $turn->getName(),
		);
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $turn->getId());
		$table->update($data, $where);
	}
	
	public function edit(Application_Model_Turnpunkte $turn, $edit)
	{
		$table = $this->getDbTable();
		
		$data = array(
				'turn' => $turn->getTurn(),
				'position' => $turn->getPosition(),
				'lat' => $turn->getLat(),
				'lng' => $turn->getLng(),
				'referenzid' => $turn->getReferenzid(),
				'referenzobjekt' => $turn->getReferenzobjekt(),
				'status' => $turn->getStatus(),
				'name' => $turn->getName()
		);
		
		$where = array(
            $table->getAdapter()->quoteInto('turn = ?', $turn->getTurn()),
            $table->getAdapter()->quoteInto('position = ?', $edit)
        );
		$table->update($data, $where);
	}


	public function fetchID($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('turnpunkte');
		$entry->where('turnpunkte.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function fetchErsteller($id)
	{
 		$db = Zend_Db_Table::getDefaultAdapter();
 		$entry = new Zend_Db_Select($db);
		$entry->from('turnpunkte');
		$entry->where('turnpunkte.ersteller = ?', $id);
		return $entry->query()->fetchall();
	}

	public function fetchTurnid($turnid)
	{
 		$db = Zend_Db_Table::getDefaultAdapter();
 		$entry = new Zend_Db_Select($db);
		$entry->from('turnpunkte');
		$entry->where('turnpunkte.turn = ?', $turnid);
		$entry->order('position ASC');
		return $entry->query()->fetchall();
	}

	public function fetchTurnidandposition($turnid, $position)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('turnpunkte');
		$entry->where('turnpunkte.turn = ?', $turnid);
		$entry->where('turnpunkte.position = ?', $position);
		$entry->order('position ASC');
		return $entry->query()->fetch();
	}
	
	public function deletallfromturnemarker($id)
	{
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('turn = ?', $id);
		$table->delete($where);
	}
	
	public function deletemarker($id, $value)
	{
		$allepunkte = $this->fetchTurnid($id);
		
		$table = $this->getDbTable();
		//$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$where = array(
            $table->getAdapter()->quoteInto('turn = ?', $id),
            $table->getAdapter()->quoteInto('position = ?', $value)
        );
		$table->delete($where);
		
		/*
		echo sizeof($allepunkte);
		foreach ($allepunkte as $array){
			echo "X";
		}
		/*
		for($i = (sizeof($allepunkte)); $i > $value; --$i){
			//$this->aendereposition($id, $i, ($i -1));
			echo $i;
		}
		*/
		$back = "allepunkte" . sizeof($allepunkte);
		for($i = $value+1; $i < (sizeof($allepunkte)+1); ++$i){
			$back = $back . " " . $i;
			$this->aendereposition($id, $i, ($i -1));
			
		}
		return $back;
		
	}
	
	public function aendereposition($id, $value, $valueneu)
	{
		$data = array(
				'position' => $valueneu);
		return $this->getDbTable()->update($data, array(
				'turn = ?' => $id, 'position = ?' => $value));
	}
	


	public function getallhaefen($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('turnpunkte', array('name AS name_turnpunkte','*'));
		$entry->where('turnpunkte.turn = ?', $id);
		$entry->where('turnpunkte.referenzobjekt  = ?', 'hafen');
		$entry->where('referenzid IS NOT NULL')->group('referenzid');
		$entry->joinleft(
				array('hafen' => 'hafen'),
				'turnpunkte.referenzid  = hafen.id');
		return $entry->query()->fetchall();
	}
}




